<template>
  <div class="container">
    <div class="app-container">
      <div class="left">
        <el-input 
          v-model="queryParams.keyword"
          style="margin-bottom:10px" 
          type="text" 
          prefix-icon="el-icon-search" 
          size="small" 
          placeholder="输入员工姓名全员搜索"
          @input="changeValue" />
        <!-- 树形组件 -->
        <el-tree 
          ref="deptTree"
          node-key="id"
          :data="depts" 
          :props="defaultProps" 
          default-expand-all
          :expand-on-click-node="false"
          highlight-current
          @current-change="selectNode">
        </el-tree>
      </div>
      <div class="right">
        <el-row class="opeate-tools" type="flex" justify="end">
          <el-button size="mini" type="primary">添加员工</el-button>
          <el-button size="mini" @click="showExcelDialog = true">excel导入</el-button>
          <el-button size="mini" @click="exportEmployee">excel导出</el-button>
        </el-row>
        <!-- 表格组件 -->
        <el-table :data="list">
          <el-table-column prop="staffPhoto" align="center" label="头像">
            <template v-slot="{ row }">
              <el-avatar v-if="row.staffPhoto" :src="row.staffPhoto" :size="30"></el-avatar>
              <span v-else class="username">{{ row.username.charAt(2) }}</span>
            </template>
          </el-table-column>
          <el-table-column prop="username" align="center" label="姓名"></el-table-column>
          <el-table-column prop="mobile" align="center" label="手机号" sortable></el-table-column>
          <el-table-column prop="workNumber" align="center" label="工号" sortable></el-table-column>
          <el-table-column prop="formOfEmployment" align="center" label="聘用形式">
            <template v-slot="{ row }">
              <span v-if="row.formOfEmployment === 1">正式</span>
              <span v-else-if="row.formOfEmployment === 2">非正式</span>
              <span v-else>无</span>
            </template>
          </el-table-column>
          <el-table-column prop="departmentName" align="center" label="部门"></el-table-column>
          <el-table-column prop="timeOfEntry" align="center" label="入职时间" sortable></el-table-column>
          <el-table-column align="center" label="操作" width="280px">
            <template v-slot="{ row }">
              <el-button type="text" size="mini">查看</el-button>
              <el-button type="text" size="mini">角色</el-button>
              <el-popconfirm 
                title="确认删除该行数据吗？"
                @onConfirm="confirmDel(row.id)">
                <el-button slot="reference" style="margin-left:10px" type="text" size="mini">删除</el-button>
              </el-popconfirm>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页 -->
        <el-row type="flex" justify="end" align="middle" style="height: 60px">
          <el-pagination
            layout="total, prev, pager, next"
            :total="total"
            :current-page="queryParams.page"
            :page-size="queryParams.pagesize"
            @current-change="changePage">
          </el-pagination>
        </el-row>
        <import-excel :show-excel-dialog.sync="showExcelDialog" @uploadSuccess="getEmployeeList"/>
      </div>
    </div>
  </div>
</template>

<script>
import { getDepartment } from '@/api/department'
import { getEmployeeList, exportEmployee, delEmployee } from '@/api/employee'
import { transListToTreeData } from '@/utils'
import FileSaver from 'file-saver'
import ImportExcel from './components/import-excel.vue'
export default {
  name: 'Employee',
  components: {
    ImportExcel
  },
  data() {
    return {
      depts: [],  // 组织数据
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      // 存储查询参数
      queryParams: {
        departmentId: null,
        page: 1,
        pagesize: 10,
        keyword: ''
      },
      total: 0, // 记录员工总数
      list: [],  // 存储员工列表数据,
      showExcelDialog: false
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    //删除员工方法
    async confirmDel(id) {
      await delEmployee(id)
      if(this.list.length === 1 && this.queryParams.page > 1) this.queryParams.page--
      this.getEmployeeList()
      this.$message.success('删除员工成功')
    },
    // 导出员工的excel
    async exportEmployee() {
      const result = await exportEmployee()
      // console.log(result)
      FileSaver.saveAs(result, '员工信息表.xlsx')
    },
    // 模糊搜索输入值内容改变时触发
    changeValue() {
      clearTimeout(this.timer) 
      this.timer = setTimeout(() => {
        this.queryParams.page = 1
        this.getEmployeeList()
      }, 300)
      
    },
    // 切换页码事件
    changePage(newPage) {
      this.queryParams.page = newPage
      this.getEmployeeList()
    },
    // 获取员工列表
    async getEmployeeList() {
      const { rows, total } = await getEmployeeList(this.queryParams)
      this.list = rows
      this.total = total
    },
    async getDepartment() {
      this.depts = transListToTreeData(await getDepartment(), 0)
      this.queryParams.departmentId = this.depts[0].id
      // 树的渲染是异步的，需要等渲染完毕后再设置这个默认选中
      this.$nextTick(() => {
        this.$refs.deptTree.setCurrentKey(this.queryParams.departmentId)
      })
      // 这个时候参数记录了id
      this.getEmployeeList()
    },
    selectNode(node) {
      this.queryParams.departmentId = node.id
      this.queryParams.page = 1
      this.getEmployeeList()
    }
  }
}
</script>

<style lang="scss" scoped>
.app-container {
  background: #fff;
  display: flex;
  .left {
    width: 280px;
    padding: 20px;
    border-right: 1px solid #eaeef4;
  }
  .right {
    flex: 1;
    padding: 20px;
    .opeate-tools {
      margin:10px ;
    }
    .username {
      height: 30px;
      width: 30px;
      line-height: 30px;
      text-align: center;
      border-radius: 50%;
      color: #fff;
      background: #04C9BE;
      font-size: 12px;
      display:inline-block;
    }
  }
}

</style>
